package xyf.p11.other.twopointer;

/**
 * ClassName: J021_OddEvenNumber
 * Description:
 * Author: xyf
 * Date: 9/9/22 10:21 AM
 * Version: 1.0
 **/
public class J021_OddEvenNumber
{
    public int[] exchange(int[] nums)
    {
        int start = 0;
        int end = nums.length - 1;

        while (start < end)
        {
            if (!isEven(nums[start]))
            {
                start++;
                continue;
            }
            if (isEven(nums[end]))
            {
                end--;
                continue;
            }

            swap(nums, start, end);
        }
        return nums;
    }

    private boolean isEven(int num)
    {
        return num % 2 == 0;
    }

    private void swap(int[] nums, int x, int y)
    {
        int tmp = nums[x];
        nums[x] = nums[y];
        nums[y] = tmp;
    }
}
